//
//  NSDate+WQCalendarLogic.m
//  iBaby365v2
//
//  Created by Kirinrin on 14-7-25.
//  Copyright (c) 2014年 suplus. All rights reserved.
//

#import "NSDate+WQCalendarLogic.h"

@implementation NSDate (WQCalendarLogic)
- (NSUInteger)numberOfDaysInCurrentMonth
{
    // 频繁调用 [NSCalendar currentCalendar] 可能存在性能问题
    return [[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:self].length;
}
- (NSDate *)firstDayOfCurrentMonth
{
    NSDate *startDate = nil;
    BOOL ok = [[NSCalendar currentCalendar] rangeOfUnit:NSMonthCalendarUnit startDate:&startDate interval:NULL forDate:self];
    NSAssert1(ok, @"Failed to calculate the first day of the month based on %@", self);
    return startDate;
}

- (NSUInteger)weeklyOrdinality
{
    return [[NSCalendar currentCalendar] ordinalityOfUnit:NSDayCalendarUnit inUnit:NSWeekCalendarUnit forDate:self];
}
- (NSUInteger)numberOfWeeksInCurrentMonth
{
    NSUInteger weekday = [[self firstDayOfCurrentMonth] weeklyOrdinality];
    
    NSUInteger days = [self numberOfDaysInCurrentMonth];
    NSUInteger weeks = 0;
    
    if (weekday > 1) {
        weeks += 1, days -= (7 - weekday + 1);
    }
    
    weeks += days / 7;
    weeks += (days % 7 > 0) ? 1 : 0;
    
    return weeks;
}

- (NSDate *)dayInThePreviousMonth{
    NSTimeInterval oneDay = 3600*24;
    NSDate *previousMonth = [NSDate dateWithTimeInterval:0-oneDay sinceDate:[self firstDayOfCurrentMonth]];
    return previousMonth;
}
- (NSDateComponents *)YMDComponents{
    return [[NSCalendar currentCalendar] components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:self];
    
}

- (NSDate *)lastDayOfCurrentMonth{

    NSRange range = [[NSCalendar currentCalendar]rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:self];
     NSDateComponents *component = [[NSCalendar currentCalendar] components:NSYearCalendarUnit|NSMonthCalendarUnit fromDate:self];
    [component setDay:range.length];
    
    return [[NSCalendar currentCalendar]dateFromComponents:component];
}
- (NSDate *)dayInTheFollowingMonth{
    NSTimeInterval oneDay = 3600*24;
    return [NSDate dateWithTimeInterval:oneDay sinceDate:[self lastDayOfCurrentMonth]];
}

- (NSString *)dayStringInMonth{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"dd"];
    return [formatter stringFromDate:self];
}

/**
 *
 *  @return 月份，如11月
 */
- (NSString *)monthStringWithChinese{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"MM月"];
    return [formatter stringFromDate:self];
}

- (NSString *)stringWithYYY_MM_dd{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    return [formatter stringFromDate:self];
}


- (BOOL)isSameMonth:(NSDate *)date{
    NSCalendar* calendar = [NSCalendar currentCalendar];
    
    unsigned unitFlags =  NSMonthCalendarUnit;
    NSDateComponents* comp1 = [calendar components:unitFlags fromDate:self];
    NSDateComponents* comp2 = [calendar components:unitFlags fromDate:date];
    
    return [comp1 month] == [comp2 month];

}

- (BOOL)isSameDay:(NSString *)dayString{
    NSCalendar* calendar = [NSCalendar currentCalendar];
    
    unsigned unitFlags =  NSDayCalendarUnit;
    NSDateComponents* comp1 = [calendar components:unitFlags fromDate:self];
    
    return comp1.day == dayString.integerValue;
}

+ (NSDate *)calendarDayWithYear:(NSUInteger)year month:(NSUInteger)month day:(NSUInteger)i{
    
    NSDateComponents *dateComponentsForDate = [[NSDateComponents alloc] init];
    [dateComponentsForDate setDay:i];
    [dateComponentsForDate setMonth:month];
    [dateComponentsForDate setYear:year];
    [dateComponentsForDate setTimeZone: [NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    
    return [[NSCalendar currentCalendar]dateFromComponents:dateComponentsForDate];
}
@end
